namespace YasharEl.Infrastructure.ApplicationModel.Bootstrap.Configuration.Internals
{
    using System.Configuration;
    using YasharEl.Infrastructure.ApplicationModel.Bootstrap.Extensibility;

    /// <summary>
    /// Default IConsumeConfigurationSection
    /// </summary>
    public class ConsumeConfigurationSection : IConsumeConfigurationSection
    {
        private readonly IConsumeConfigurationSection consumer;

        /// <summary>
        /// Initializes a new instance of the <see cref="ConsumeConfigurationSection"/> class.
        /// </summary>
        /// <param name="extension">The extension.</param>
        public ConsumeConfigurationSection(IBootstrappingExtension extension)
        {
            var consumerCandidate = extension as IConsumeConfigurationSection;
            this.consumer = consumerCandidate ?? new NullConsumer();
        }

        /// <summary>
        /// The configuration section which is consumed.
        /// </summary>
        /// <param name="section">The configuration section.</param>
        public void Apply(ConfigurationSection section)
        {
            this.consumer.Apply(section);
        }

        /// <summary>
        /// Consumer which does nothing with the applied configuration section.
        /// </summary>
        private class NullConsumer : IConsumeConfigurationSection
        {
            /// <inheritdoc />
            public void Apply(ConfigurationSection section)
            {
            }
        }
    }
}